from django.conf import settings
from django.http import Http404
from django.shortcuts import render
from django.views.decorators.http import require_GET, require_POST

# from rest_framework import
from apps.news.form import CommentForm
from apps.news.models import News, NewsCategory, NewsComment, Focus
from apps.news.serializers import NewsSerializer, CommentSerializer
from apps.xfzauth.decorators import xfz_login_required
from utils import restful


def index(request):
    newses = News.objects.select_related('category', 'author')[0: settings.NEWS_PAGE_LIMIT]
    categories = NewsCategory.objects.all()
    focuses = Focus.objects.all()
    context = {
        'newses': newses,
        'categories': categories,
        'focuses': focuses,
    }
    return render(request, 'news/index.html', context=context)


def detail_view(request, news_id):
    try:
        news = News.objects.select_related('category', 'author').get(pk=news_id)
        comments = NewsComment.objects.select_related('author').filter(news=news)[0:settings.NEWS_COMMENT_LIMIT]
        context = {
            'news': news,
            'comments': comments,
        }
        return render(request, 'news/news_detail.html', context=context)
    except News.DoesNotExist as e:
        raise Http404


def search(request):
    return render(request, 'search/search.html')


@require_GET
def list_view(request):
    page = int(request.GET.get('p', 1))
    category_id = int(request.GET.get('c', 0))

    start = (page - 1) * settings.NEWS_PAGE_LIMIT
    end = start + settings.NEWS_PAGE_LIMIT

    if category_id:
        newses = News.objects.select_related('category', 'author').filter(category_id=category_id)[start: end]
    else:
        newses = News.objects.select_related('category', 'author').all()[start: end]
    serializer = NewsSerializer(newses, many=True)

    return restful.ok(data=serializer.data)


@xfz_login_required
@require_POST
def comment_view(request):
    form = CommentForm(request.POST)
    if form.is_valid():
        content = form.cleaned_data.get('content')
        news_id = form.cleaned_data.get('news')
        try:
            news = News.objects.get(pk=news_id)
            comment = NewsComment.objects.create(content=content, news=news, author=request.user)
            comment = CommentSerializer(comment)
            return restful.ok(message='评论成功~~', data=comment.data)
        except:
            return restful.params_error(message='该文章不存在~~')
    else:
        return restful.params_error(message=form.get_error())


@require_GET
def list_comment_view(request, news_id):
    page = int(request.GET.get('p', 1))

    start = (page - 1) * settings.NEWS_PAGE_LIMIT
    end = start + settings.NEWS_PAGE_LIMIT
    if news_id:
        comments = NewsComment.objects.select_related('author').filter(news_id=news_id)[start: end]
    else:
        return restful.params_error(message='该新闻已删除~~')
    serializer = CommentSerializer(comments, many=True)

    return restful.ok(data=serializer.data)
